package com.zsyh.changshang.basic;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;


import com.google.common.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author shichen 754314442@qq.com
 * Created by Administrator on 2018/9/28.
 */
public abstract class BaseActivity<V extends BaseContract.View, P extends BaseContract.Presenter<V>> extends AppCompatActivity implements BaseContract.View {
    protected P presenter;
    protected Unbinder mUnbinder;
    protected final String TAG = getClass().getSimpleName();
    private NetWorkStateReceiver mNetWorkStateReceiver;
    private boolean isRegistered = false;

    protected final String[] months = new String[] {
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
    };

    protected final String[] parties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };

    private static final int PERMISSION_STORAGE = 0;

    protected Typeface tfRegular;
    protected Typeface tfLight;



    @SuppressWarnings("unchecked")
    @CallSuper
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (null != savedInstanceState)
            savedInstanceState = null;
        super.onCreate(savedInstanceState);
        //使用注解的方式，添加布局以及presenter

        int layoutResId = getClass().getAnnotation(Viewable.class).layout();
        if (layoutResId != Viewable.LAYOUT_NOT_DEFINED) {
            setContentView(layoutResId);
            mUnbinder = ButterKnife.bind(this);
        }
        presenter = initPresenter();
        presenter.attachView((V) this);
        checkNotNull(presenter);
        presenter.onPresenterCreated();
        registerNewWorkStateReceiver();

       /* tfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
        tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");*/


    }

    private boolean netWorkUnusableShowed = false;
    private void registerNewWorkStateReceiver() {
        mNetWorkStateReceiver = NetWorkChangedFactory.getInstance(new NetWorkStateReceiver.OnNetWorkChangedListener() {
            @Override
            public void onNetWorkUsable() {
                if (netWorkUnusableShowed) {
                    //shortToast("网络已正常连接");
                    //TODO 监听手机联网状态 此处可以做一些恢复联网之后的逻辑
                    netWorkUnusableShowed = false;
                }
            }

            @Override
            public void onNetWorkUnusable() {
                if (!netWorkUnusableShowed) {
                    //shortToast("网络不可用");
                    //TODO 监听手机联网状态 此处可以做一些断网之后的逻辑
                    netWorkUnusableShowed = true;
                }
            }
        }).getReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mNetWorkStateReceiver, filter);
        isRegistered = true;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isNeedCheck) {
            //通过注解反射得到该页面需要的权限
            String[] needPermissions = getClass().getAnnotation(Viewable.class).needPermissions();
            if (needPermissions.length > 0) {
                checkPermissions(needPermissions);
            }
        }
    }





    @Override
    protected void onPause() {
        super.onPause();
       // MobclickAgent.onPause(this);
    }

    @CallSuper
    @Override
    protected void onDestroy() {
        super.onDestroy();


        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        presenter.detachView();
        //解绑
        if (isRegistered) {
            unregisterReceiver(mNetWorkStateReceiver);
        }
        //页面暂停时就取消所有订阅
        unSubscribe();

    }

    @SuppressWarnings("unchecked")
    protected P initPresenter() {
        return (P) AnnotationHelper.createPresenter(getClass());
    }

    @Override
    public void showLoading() {
        //清除已经存在的，同样的fragment
     /*   FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag(LoadingDialog.class.getSimpleName());
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        LoadingDialog newFragment = LoadingDialog.getInstance();
        newFragment.show(ft, LoadingDialog.class.getSimpleName());*/
    }

    @Override
    public void disMissLoading() {
     /*   Fragment prev = getSupportFragmentManager().findFragmentByTag(LoadingDialog.class.getSimpleName());
        if (prev != null) {
            LoadingDialog.getInstance().dismiss();
        }*/
    }

    @Override
    public void shortToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void logMsg(String TAG, String msg, Throwable e) {
        LogUtilsMiao.Log(TAG, msg, e);
    }

    @Override
    public void logMsg(String TAG, String msg) {
        LogUtilsMiao.Log(TAG, msg);
    }

    @Override
    public Context getContext() {
        return getBaseContext();
    }

    @Override
    public void setStatusColor(int color) {
        StatusUtil.setUseStatusBarColor(this, color);
    }

    @Override
    public void setImmerse(boolean immerse, boolean black) {
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, immerse, black);
    }

    private List<Disposable> mDisposables = new ArrayList<>();

    @Override
    public void subscribe(Disposable d) {
        mDisposables.add(d);
    }

    /**
     * 取消所有订阅
     */
    @Override
    public void unSubscribe() {
        if (mDisposables.size() > 0) {
            for (int i = 0; i < mDisposables.size(); i++) {
                if (mDisposables.get(i) != null) {
                    if (!mDisposables.get(i).isDisposed()) {
                        mDisposables.get(i).dispose();
                    }
                }
            }
        }
    }

    private static final int PERMISSON_REQUESTCODE = 0;

    /**
     * 判断是否需要检测，防止不停的弹框
     */
    private boolean isNeedCheck = true;

    @Override
    public void checkPermissions(String... permissions) {
        List<String> needRequestPermissonList = findDeniedPermissions(permissions);
        if (null != needRequestPermissonList
                && needRequestPermissonList.size() > 0) {
            ActivityCompat.requestPermissions(this,
                    needRequestPermissonList.toArray(
                            new String[needRequestPermissonList.size()]),
                    PERMISSON_REQUESTCODE);
        } else {
            allPermissionsOk(permissions);
        }
    }

    /**
     * 获取权限集中需要申请权限的列表
     *
     * @param permissions
     * @return
     * @since 2.5.0
     */
    private List<String> findDeniedPermissions(String... permissions) {
        List<String> needRequestPermissionList = new ArrayList<String>();
        for (String perm : permissions) {
            if (ContextCompat.checkSelfPermission(this,
                    perm) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.shouldShowRequestPermissionRationale(
                    this, perm)) {
                needRequestPermissionList.add(perm);
            }
        }
        return needRequestPermissionList;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSON_REQUESTCODE) {
            if (!verifyPermissions(grantResults)) {
                permissionsDenied(deniedPermissions(permissions, grantResults));
                isNeedCheck = false;
            } else {
                allPermissionsOk(permissions);
            }
        }
    }

    /**
     * 检测是否说有的权限都已经授权
     *
     * @param grantResults 授权结果
     * @return
     * @since 2.5.0
     */
    private boolean verifyPermissions(int[] grantResults) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    private List<String> deniedPermissions(@NonNull String[] permissions, @NonNull int[] grantResults) {
        List<String> deniedPermissionList = new ArrayList<>();
        for (int index = 0; index < grantResults.length; index++) {
            if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
                deniedPermissionList.add(permissions[index]);
            }
        }
        return deniedPermissionList;
    }

    @Override
    public void allPermissionsOk(String[] permissions) {
        LogUtilsMiao.Log(TAG, "allPermissionsOk");
    }

    @Override
    public void permissionsDenied(List<String> deniedPermissions) {
        LogUtilsMiao.Log(TAG, "permissionsDenied");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
    }

    @Override
    public void onFailed(@Nullable String msg, @Nullable Throwable e) {
        if (!TextUtils.isEmpty(msg)){
            shortToast(msg);
        }else {
            if (e!=null){

            }
        }
    }




    /**
     * 检查是否是正确手机号
     *
     * @param phoneNumber
     * @return 手机号码
     * 移动：134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188、198
     * 联通：130、131、132、145、155、156、166、175、176、185、186
     * 电信：133、149、153、173、177、180、181、189、199
     */
    public static boolean isPhoneNumber(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return false;
        }
        String reg = "^((13[0-9])|(14[5,7,9])|(15[0-9])|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(phoneNumber);
        return matcher.matches();
    }



    /**
     * 设置手机屏幕亮度变暗
     */
    public void lightoff() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f;
        getWindow().setAttributes(lp);
    }


    /**
     * 设置手机屏幕亮度显示正常
     */
    public void lighton() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1f;
        getWindow().setAttributes(lp);
    }

    /**
     * 调用拨号界面
     */
    public void call(String phone) {//String phone
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //邮箱验证
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        if (TextUtils.isEmpty(strPattern)) {
            return false;
        } else {
            return strEmail.matches(strPattern);
        }
    }





}
